﻿namespace WebCoreEmptyToMiddleWare
{
    public class 手写实现MVC
    {

        public void InvokeMVC(string[] args) {
            var builder = WebApplication.CreateBuilder(args);
            var app = builder.Build();
            app.MapGet("/", () => "Hello World!");
            app.Map("/test", appbuild => {
                appbuild.Use(async (context, next) => {
                    context.Response.ContentType = "text/html";
                    await context.Response.WriteAsync("1 start </br>");
                    await next.Invoke();
                    await context.Response.WriteAsync("1 end </br>");
                });
                appbuild.UseMiddleware<SelfMiddleware>();// 添加中间件

                appbuild.Use(async (context, next) => {
                    await context.Response.WriteAsync("2 start </br>");
                    await next.Invoke();
                    await context.Response.WriteAsync("2 end </br>");
                });


                appbuild.Run(async context => {
                    await context.Response.WriteAsync("Run Fly alway </br>");
                    await context.Response.WriteAsync("get Items Value：" + context.Items["Key1"]);
                });
            });
            app.MapWhen(context => context.Request.Query.ContainsKey("branch"), branch =>
            { // 自定义的条件谓词动态选择是否应用指定的子管道
                branch.Run(async context =>
                {
                    await context.Response.WriteAsync("This is a conditional branch.");
                });
            });


            app.Run();
        }
    }
}
